package SocketDemo.URL;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author:周鹏
 * @date: 2020/11/9 19:51
 * @description:通过url获取网络资源
 */
public class CatchSource {

    public static void main(String[] args) throws Exception{
        URL url=new URL("https://m10.music.126.net/20201117175237/3448a0a052ccd2ea70db5ddc05d17469/ymusic/5252/510b/000e/e8e8f3127cfaae4471477d876ab63047.mp3");
        HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
        InputStream is=httpURLConnection.getInputStream();
        FileOutputStream fileOutputStream=new FileOutputStream(new File("xxx.mp3"));
        int len=0;
        byte[] buffer=new byte[1024];
        while((len=is.read(buffer))!=-1){
                    fileOutputStream.write(buffer,0,len);

        }
        is.close();
        fileOutputStream.close();
        httpURLConnection.disconnect();
    }

}
